//
//  ViewController.m
//  项目三
//
//  Created by rimi on 15/9/24.
//  Copyright (c) 2015年 rimi. All rights reserved.
//
#define BUTTONTAG 100
#import "ViewController.h"
#import "JLTableViewCell.h"
#import "LoginView.h"

static NSString*const kUITableViewCellIndentifier = @"cell";
@interface ViewController () <UITableViewDataSource,UITableViewDelegate> {
    
    NSArray *_dataSource;
    NSArray *_dataSource_one;
    NSArray *_imageDataSource;
    NSArray *_imageDataSource_one;
    UIButton *_btn_one;
    UIImageView *_imageView;
    UIImageView *_headImageView;
    UILabel *_label;
}

@property (nonatomic, strong) UIView *leadView;
@property (nonatomic, strong) UIImageView *backgroundImage;
@property (nonatomic, strong) UIImageView *logoOne;
@property (nonatomic, strong) UIImageView *logoTwo;
@property (nonatomic, strong) UIImageView *logoThree;
@property (nonatomic, strong) UILabel *labelOne;

@property (nonatomic, strong) UIViewController *mainViewController;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;

//- (void)initializeAppearence;
- (void)initializeUserInterface; /**< 初始化用户界面 */
@end

@implementation ViewController
#pragma mark - init
- (instancetype)initWithMainViewController:(UIViewController *)mainViewController {
    
    self = [super init];
    if (self) {
        self.mainViewController = mainViewController;
    }
    return self;
}
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
    
    
}
- (void)initializeUserInterface {
    
    
    _dataSource = @[@"首页-1",@"金融资讯-1",@"理财服务-1",@"银行卡-1",@"缴费服务-1",@"代理产品-1",@"我的网银-1",@"活动交流-1",@"日程提醒-1",@"业务咨询-1"];
    _imageDataSource = @[@"首页",@"金融资讯",@"理财服务",@"银行卡",@"缴费服务",@"代理产品",@"我的网银",@"活动交流",@"日程提醒",@"业务咨询"];
    _dataSource_one = @[@"首页-1",@"金融资讯-1",@"理财服务-1",@"代理产品-1",@"我的网银-1"];
    _imageDataSource_one = @[@"首页",@"金融资讯",@"理财服务",@"代理产品",@"我的网银"];
    
    self.mainViewController.view.frame = DHFlexibleFrame(CGRectMake(60, 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT - 20));
    self.view.backgroundColor = COLOR(106, 70, 4, 1);
    
    
    [self.view addSubview:self.tableView];
    _headImageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(8, 8, 45, 45))];
    _headImageView.image = [UIImage imageNamed:@"头像"];
    [self.view addSubview:_headImageView];
    
    _label = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(80, 17, 114, 37))];
    _label.text = @"尊敬的用户,下午好";
    _label.textColor = [UIColor whiteColor];
    _label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:_label];
    
    UILabel *sliderLabel = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(69, 944, 68, 18))];
    sliderLabel.text = @"Ver:1.1.0";
    sliderLabel.textColor = [UIColor lightGrayColor];
    sliderLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:sliderLabel];
    
    /**< 抽屉开关 */
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:DHFlexibleFrame(CGRectMake(0, 924, 260, 70))];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    btn.tag = BUTTONTAG;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = DHFlexibleFrame(CGRectMake(0, 924, 62, 70));
    imageView.image = [UIImage imageNamed:@"滑动-1"];
    [self.view addSubview:imageView];
    
    
    /**< 设置 */
    _btn_one = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btn_one setFrame:DHFlexibleFrame(CGRectMake(0, 782, 260, 70))];
    [_btn_one setBackgroundColor:[UIColor clearColor]];
    [_btn_one addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn_one];
    _btn_one.tag = BUTTONTAG+1;
    
    UIImageView *imageView0 = [[UIImageView alloc] init];
    imageView0.frame = DHFlexibleFrame(CGRectMake(0, 0, 62, 70));
    imageView0.image = [UIImage imageNamed:@"设置-1"];
    [_btn_one addSubview:imageView0];
    
    UIImageView *imageView1 = [[UIImageView alloc] init];
    imageView1.frame = DHFlexibleFrame(CGRectMake(69, 24, 39, 22));
    imageView1.image = [UIImage imageNamed:@"设置"];
    [_btn_one addSubview:imageView1];
    
    /**< 登录 */
    UIButton *btn_two = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_two setFrame:DHFlexibleFrame(CGRectMake(0, 852, 260, 70))];
    [btn_two setBackgroundColor:[UIColor clearColor]];
    [btn_two addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_two];
    btn_two.tag = BUTTONTAG + 2;
    
    UIImageView *imageView2 = [[UIImageView alloc] init];
    imageView2.frame = DHFlexibleFrame(CGRectMake(0, 0, 62, 70));
    imageView2.image = [UIImage imageNamed:@"登录-1"];
    [btn_two addSubview:imageView2];
    
    _imageView = [[UIImageView alloc] init];
    _imageView.frame = DHFlexibleFrame(CGRectMake(69, 24, 39, 22));
    _imageView.image = [UIImage imageNamed:@"登录"];
    [btn_two addSubview:_imageView];
    
    [self addChildViewController:self.mainViewController];
    
    [self.view addSubview:self.leadView];
    [self.leadView addSubview:self.backgroundImage];
    [self.leadView addSubview:self.logoOne];
    [self.leadView addSubview:self.logoTwo];
    [self.leadView addSubview:self.logoThree];
    [self.leadView addSubview:self.labelOne];
    [self animationWithLogo];
}

- (void)respondsToButton:(UIButton *)sender {
    
    if (sender.tag == 100) {
        sender.selected = !sender.selected;
        if (sender.selected) {
            [self openMainVc];
        } else {
            
            [self closeMainVc];
        }
    } else if (sender.tag == 101) {
        
        [[[ControllerManager sharedManager] mainViewController] selectControllerAtIndex:10];
    } else {
        
        LoginView *loginV = [[LoginView alloc] init];
    }
}
- (void)actionOnMainVc:(UIPanGestureRecognizer *)panGestureRecognizer {
    
    
}
#pragma mark - system methods
#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

#warning 调试登录和非登录界面
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([UserModel defaultUser].isLogin == NO) {
        return 5;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIndentifier];
    if (!cell) {
        cell = [[JLTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIndentifier];
    }
    cell.backgroundColor = COLOR(106, 70, 4, 1);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
#warning
    if ([UserModel defaultUser].isLogin == NO) {
        
        cell.nameImageView.image = [UIImage imageNamed:_imageDataSource_one[indexPath.row]];
        cell.headImageView.image = [UIImage imageNamed:_dataSource_one[indexPath.row]];
        _btn_one.hidden = YES;
        
    } else {
        
        
        _btn_one.hidden = NO;
        cell.nameImageView.image = [UIImage imageNamed:_imageDataSource[indexPath.row]];
        cell.headImageView.image = [UIImage imageNamed:_dataSource[indexPath.row]];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[ControllerManager sharedManager].mainViewController selectControllerAtIndex:indexPath.row];
    
}
#pragma mark - override
- (void)addChildViewController:(UIViewController *)childController {
    [self.view addSubview:childController.view];
    [super addChildViewController:childController];
    [childController didMoveToParentViewController:childController];
}

#pragma mark - interface methods
- (void)openMainVc {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.mainViewController.view.frame = DHFlexibleFrame(CGRectMake(228 , 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT - 20));
        
    }];
}

- (void)closeMainVc {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.mainViewController.view.frame = DHFlexibleFrame(CGRectMake(60, 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT - 20));
    }];
}

#pragma mark - animation

- (void)animationWithLogo {
    
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"opacity";
    animation.fromValue = @1;
    animation.toValue = @0;
    animation.duration = 2;
    animation.delegate = self;
    animation.removedOnCompletion = NO;
    [self.logoOne.layer addAnimation:animation forKey:@"logoOne"];
    [self.logoThree.layer addAnimation:animation forKey:@"logoThree"];
    [self.labelOne.layer addAnimation:animation forKey:@"label"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    CAAnimation *animation = [self.logoOne.layer animationForKey:@"logoOne"];
    if (animation == anim) {
        
        //移除
        [self.logoOne removeFromSuperview];
        [self.logoThree removeFromSuperview];
        [self.labelOne removeFromSuperview];
        
        //位置移动
        CABasicAnimation *positionAnimation = [CABasicAnimation animation];
        positionAnimation.keyPath = @"position";
        positionAnimation.fromValue = [NSValue valueWithCGPoint:self.logoTwo.center];
        positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(98.5, 38.5)];
        positionAnimation.duration = 1;
        positionAnimation.removedOnCompletion = NO;
        positionAnimation.delegate = self;
        
        
        //缩小
        CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
        scaleAnimation.keyPath = @"transform.scale";
        scaleAnimation.toValue = @(0.62);
        scaleAnimation.duration = 1;
        scaleAnimation.removedOnCompletion = NO;
        scaleAnimation.delegate = self;
        
        
        CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
        groupAnimation.animations = @[positionAnimation, scaleAnimation];
        groupAnimation.duration = 1;
        groupAnimation.delegate = self;
        groupAnimation.removedOnCompletion = NO;
        groupAnimation.fillMode = kCAFillModeForwards;
        
        [self.logoTwo.layer addAnimation:groupAnimation forKey:@"logoTwo"];
        
        
    }
    
    animation = [self.logoTwo.layer animationForKey:@"logoTwo"];
    if (animation == anim) {
        
        CABasicAnimation *animation = [CABasicAnimation animation];
        animation.keyPath = @"opacity";
        animation.fromValue = @1;
        animation.toValue = @0;
        animation.duration = 2;
        animation.removedOnCompletion = NO;
        animation.delegate = self;
        [self.backgroundImage.layer addAnimation:animation forKey:@"backgroundImage"];
        
        
    }
    
    animation = [self.backgroundImage.layer animationForKey:@"backgroundImage"];
    
    if (animation == anim) {
        
        [self.leadView removeFromSuperview];
        [self.logoTwo removeFromSuperview];
        
    }
    
}
#pragma mark - getter
- (UIView *)leadView {
    
    if (!_leadView) {
        _leadView = ({
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
            
            
            view;
        });
    }
    return _leadView;
}
- (UIImageView *)backgroundImage {
    
    if (!_backgroundImage) {
        _backgroundImage = ({
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
            imageView.image = [UIImage imageNamed:@"背景2"];
            
            imageView;
        });
    }
    return _backgroundImage;
}
- (UIImageView *)logoOne {
    
    if (!_logoOne) {
        _logoOne = ({
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(143, 400, 100, 33)];
            imageView.image = [UIImage imageNamed:@"工行LOGO11"];
            
            imageView;
        });
    }
    return _logoOne;
}
- (UIImageView *)logoTwo {
    
    if (!_logoTwo) {
        _logoTwo = ({
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(265, 390, 60, 60)];
            imageView.image = [UIImage imageNamed:@"工行LOGO22"];
            imageView.layer.cornerRadius = 30;
            imageView.clipsToBounds = YES;
            imageView;
        });
    }
    return _logoTwo;
}
- (UIImageView *)logoThree {
    
    if (!_logoThree) {
        _logoThree = ({
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(355, 400, 260, 47)];
            imageView.image = [UIImage imageNamed:@"工行LOGO33"];
            
            imageView;
        });
    }
    return _logoThree;
}
- (UILabel *)labelOne {
    
    if (!_labelOne) {
        _labelOne = ({
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(145, 470, 450, 35)];
            label.text = @"中国工商银行财富管家系统";
            label.font = [UIFont boldSystemFontOfSize:35];
            label.textAlignment = NSTextAlignmentCenter;
            label;
        });
        
    }
    return _labelOne;
}

- (UIPanGestureRecognizer *)panGestureRecognizer {
    
    if (!_panGestureRecognizer) {
        _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionOnMainVc:)];
    }
    return _panGestureRecognizer;
}
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 45, 260, ORIGIN_HEIGHT - 100)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70;
        _tableView.backgroundColor = COLOR(106, 70, 4, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
@end
